package com.lkm.app_ui.ui;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.lkm.app_ui.AppConst;
import com.lkm.app_ui.MBaseFragment;
import com.lkm.app_ui.R;
import com.lkm.app_ui.RouterUINavigation;
import com.lkm.app_ui.RunTimeData;
import com.lkm.app_ui.help.AppUIHelp;
import com.lkm.app_ui.m.fun.OnKeyNumberEvent;
import com.lkm.app_ui.m.goods.BarcodeResultBO;
import com.lkm.frame.otto.LinkBusCycleWrapper;
import com.lkm.frame.otto.ObserverAutoNext;
import com.lkm.frame.util.CycleHelp;
import com.lkm.javatool.NumberHelp;
import com.lkm.javatool.StringHelp;
import com.lkm.market2_0.help.SteelardHelp;
import com.lkm.market2_0.m.steelyarddata.OrderEntity;
import com.lkm.market2_0.m.steelyarddata.SteelyardDataModule;
import com.lkm.market2_0.m.steelyardserver.OnSteelyardKeyEvent;
import com.lkm.market2_0.m.steelyardserver.to.WeightChangeInfo;
import com.lkm.market2_0.m.steelyardserver.event.OnWeightInfoChangeEvent;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;



public class BarcodeResultFragment extends MBaseFragment {


    public static BarcodeResultFragment getInstance() {
        BarcodeResultFragment f = new BarcodeResultFragment();





        return f;
    }

    public static BarcodeResultFragment instance;



    public BarcodeResultFragment() {
        instance = this;
    }

    @Override
    protected int configLayoutResID() {
        return R.layout.fragment_barcode_result;
    }

    @Override
    protected boolean configIsLockKey() {
        return true;
    }


    private float unitPrice1 = -1 ;
    private String name1 = "" ;



    private boolean isBarcodeResult = true;


    private BarcodeResultBO barcodeResultBO;


    private WeightChangeInfo weightChangeInfo;

    TextView tv_name1,tv_weight1,tv_price1,tv_money;

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        interceptedKeyAndNumberEvent();
        reOnWeightInfoChangeEvent();


        tv_name1 = ((TextView) findViewById(R.id.tv_name1));//.setText(weightChangeInfo.getName());
        tv_weight1 = ((TextView) findViewById(R.id.tv_weight1));//.setText(NumberHelp.formatMoney(weightChangeInfo.getWeight(), 3, "--"));
        tv_price1 = ((TextView) findViewById(R.id.tv_price1));//.setText(SteelardHelp.formatMoney(weightChangeInfo.getUnitPrice()));
        tv_money = ((TextView) findViewById(R.id.tv_money));

        ((TextView) findViewById(R.id.tv_weight_unit)).setText("("+RunTimeData.getAppWeightUnit()+")");


        ((TextView)findViewById(R.id.tv_tips)).setText("确定：'累计'按钮\n\n取消：'功能选择'按钮");
        if(isBarcodeResult){
            ((TextView)findViewById(R.id.tv_tips)).append ("\n\n计件切换：'商品切换'按钮");
        }

        String wunit = RunTimeData.getAppWeightUnit();

        ((TextView)findViewById(R.id.tv_tips_price1)).setText("(元/"+wunit+") 请输入单价");

        AppUIHelp.inputUI(tv_price1);

        if(isBarcodeResult){
            barcodeResultBO = RunTimeData.barcodeResultBO ;
            if(!onBarcodeResultBO()){

                AppUIHelp.tryShowTipsLong("无对应的商品。");
                RouterUINavigation.getInstance().dismiss(this);
            }
        }
    }




    private boolean onBarcodeResultBO(){
        if(barcodeResultBO==null)return false;

        unitPrice1 = StringHelp.toFloat(barcodeResultBO.price, unitPrice1);
        name1 = barcodeResultBO.goodsEntity==null?"":barcodeResultBO.goodsEntity.name;

        return barcodeResultBO.goodsEntity!=null;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();


        if (instance == this)
            instance = null;

    }

    private void reOnWeightInfoChangeEvent() {
        LinkBusCycleWrapper.register(new ObserverAutoNext<OnWeightInfoChangeEvent>() {
            @Override
            public Class<OnWeightInfoChangeEvent> getBusEventClass() {
                return OnWeightInfoChangeEvent.class;
            }

            @Override
            public void onReceiver(OnWeightInfoChangeEvent data) {
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        weightChangeInfo = data.getWeightChangeInfo();
                        if (isExit()) return;
                        if(unitPrice1 ==-1)
                            unitPrice1 = weightChangeInfo.getUnitPrice();
                        upWeightInfo(weightChangeInfo);

                    }
                });

            }
        }, holdCycleHelp(), CycleHelp.CycleType.Destroy);
    }

    private void upWeightInfo(WeightChangeInfo weightChangeInfo){

        weightChangeInfo.setUnitPrice(unitPrice1);
        weightChangeInfo.setTotalPrice(SteelardHelp.calPrice(weightChangeInfo.getUnitPrice(), weightChangeInfo.getWeight()));

        if(!StringHelp.isEmpty(name1)){
            weightChangeInfo.setName(name1);
        }

        tv_name1.setText(weightChangeInfo.getName());
        tv_price1.setText(SteelardHelp.formatMoney(weightChangeInfo.getUnitPrice()));
        tv_money.setText(SteelardHelp.formatMoney(weightChangeInfo.getTotalPrice()));
        tv_weight1.setText(NumberHelp.formatMoney(weightChangeInfo.getWeight(), 3, "--"));
    }



    private void interceptedKeyAndNumberEvent() {
        LinkBusCycleWrapper.register(new ObserverAutoNext<OnSteelyardKeyEvent>() {
            @Override
            public Class<OnSteelyardKeyEvent> getBusEventClass() {
                return OnSteelyardKeyEvent.class;
            }

            @Override
            public void onReceiver(OnSteelyardKeyEvent data) {
                if (!data.isIntercepted()) {
                    data.setIntercepted(true);
                    if (data.getKeyCode() == OnSteelyardKeyEvent.KEYCODE_FUN) {
                        RouterUINavigation.getInstance().dismiss(BarcodeResultFragment.this);
                    } else if (data.getKeyCode() == OnSteelyardKeyEvent.KEYCODE_LEIJI) {

                        if(weightChangeInfo == null)return;

                        SteelyardDataModule steelyardDataModule = (SteelyardDataModule)AppConst.getModuleManager().peekModule(SteelyardDataModule.class);
                        if(steelyardDataModule!=null){
                            upWeightInfo(weightChangeInfo);
                            OrderEntity.GoodEntity goodEntity = OrderEntity.GoodEntity.toGoodEntity(RunTimeData.steelyardInfo, weightChangeInfo, new OrderEntity.GoodEntityEx());

                            if (barcodeResultBO != null && barcodeResultBO.goodsEntity != null) {
//                                goodEntity.addExtData("goods", barcodeResultBO.goodsEntity);
                                goodEntity.goodCode = barcodeResultBO.goodsEntity.code;
                                goodEntity.goodId = barcodeResultBO.goodsEntity.id;
                            }

                            steelyardDataModule.subtotal(RunTimeData.LoaclSteelyardMac, goodEntity);
                        }
                        RouterUINavigation.getInstance().dismiss(BarcodeResultFragment.this);

                    } else if (data.getKeyCode() == OnSteelyardKeyEvent.KEYCODE_GOODQIEH) {
                        if(isBarcodeResult) {

                            RouterUINavigation.getInstance().dismiss(BarcodeResultFragment.this);
                            RouterUINavigation.getInstance().showCountNum( true);
                        }
                    }
                }

            }
        }, holdCycleHelp(), CycleHelp.CycleType.Destroy);



        LinkBusCycleWrapper.register(new ObserverAutoNext<OnKeyNumberEvent>() {
            @Override
            public Class<OnKeyNumberEvent> getBusEventClass() {
                return OnKeyNumberEvent.class;
            }

            boolean isOnce = false;
            @Override
            public void onReceiver(OnKeyNumberEvent data) {
                if (!data.isIntercepted()) {
                    data.setIntercepted(true);
                    if(!isOnce&&data.isFinally()){

                        isOnce = true;
                        return;
                    }
                    isOnce = true;

                    unitPrice1 = data.getNumber();
                    if(weightChangeInfo!=null){
                        upWeightInfo(weightChangeInfo);
                    }

//                    tv_weight1.setText(NumberHelp.formatMoney(weightChangeInfo.getWeight(), 3, "--"));

                }
            }
        }, holdCycleHelp(), CycleHelp.CycleType.Destroy);
    }






}
